package com.youtui.manager.controller;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.SessionKey;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.apache.shiro.web.session.mgt.WebSessionKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.youtui.common.constant.ResultMessage;
import com.youtui.common.constant.SystemConstant;
import com.youtui.common.util.Md5Util;
import com.youtui.common.util.NextIdUtil;
import com.youtui.common.util.ResultUtil;
import com.youtui.manager.model.Opter;
import com.youtui.manager.service.IOpterService;

@Controller
@RequestMapping("/opter")
public class OpterController extends BaseController {

	@Autowired
	private IOpterService opterService;

	@RequestMapping("/pagelist.do")
	@ResponseBody
	public ResultMessage pagelist(Opter opter, @RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "20") Integer rows) {

		if (!isManager()) {
			return ResultUtil.result(ResultUtil.code_fail, "暂无权限", null);
		}

		PageInfo<Opter> pageinfo = opterService.findOpterPageList(opter, page, rows);
		return ResultUtil.result(ResultUtil.code_success, "操作员信息", pageinfo);

	}

	@RequestMapping("/load.do")
	@ResponseBody
	public ResultMessage load() {
		Opter opter = null;
		String sessionid = request.getSession().getId();
		SessionKey key = new WebSessionKey(sessionid, request, response);
		try {
			Session se = SecurityUtils.getSecurityManager().getSession(key);
			Object obj = se.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
			SimplePrincipalCollection coll = (SimplePrincipalCollection) obj;
			opter = (Opter) coll.getPrimaryPrincipal();
		} catch (Exception e) {
			// e.printStackTrace();
			return ResultUtil.result(ResultUtil.code_fail, "操作员获取失败");
		} finally {
		}
		return ResultUtil.result(ResultUtil.code_success, "操作员信息", opter);
	}

	@RequestMapping("/add.do")
	@ResponseBody
	public ResultMessage add(Opter opter) {
		if (!isManager()) {
			return ResultUtil.result(ResultUtil.code_fail, "暂无权限");
		}
		if (opter.getId() != null) {
			return ResultUtil.result(ResultUtil.code_fail, "新增操作员错误ID已存在");
		}
		if (StringUtils.isBlank(opter.getLoginname())) {
			return ResultUtil.result(ResultUtil.code_fail, "登录账号不能为空");
		}
		if (StringUtils.isBlank(opter.getPwd())) {
			return ResultUtil.result(ResultUtil.code_fail, "登录密码不能为空");
		}
		Opter optertmp = opterService.findOpterByLoginName(opter.getLoginname());

		if (optertmp != null) {
			return ResultUtil.result(ResultUtil.code_fail, "登录账号已存在");
		}

		String pwd = opter.getPwd();
		opter.setId(NextIdUtil.getNextId());
		opter.setStatus(SystemConstant.status_effect);
		opter.setPwd(Md5Util.md5(opter.getPwd()));
		opter.setIsmanage(0);
		opterService.add(opter);

		return ResultUtil.result(ResultUtil.code_success, "操作员信息添加成功，登录密码：" + pwd);
	}

	@RequestMapping("/updatePwd.do")
	@ResponseBody
	public ResultMessage updatePwd(Long id, String oldPwd, String newPwd) {

		if (id == null || StringUtils.isBlank(newPwd) || StringUtils.isBlank(oldPwd)) {
			return ResultUtil.result(ResultUtil.code_fail, "请求参数错误");
		}
		if (oldPwd.equals(newPwd)) {
			return ResultUtil.result(ResultUtil.code_fail, "修改前后密码不能一致");
		}

		Opter opter = opterService.findOpterById(id);

		if (opter == null) {
			return ResultUtil.result(ResultUtil.code_fail, "操作员信息不存在");
		}

		if (!opter.getPwd().equals(Md5Util.md5(oldPwd))) {
			return ResultUtil.result(ResultUtil.code_fail, "原始密码错误");
		}

		opter.setPwd(Md5Util.md5(newPwd));
		opterService.update(opter);

		return ResultUtil.result(ResultUtil.code_success, "密码修改成功");
	}

	@RequestMapping("/resetPwd.do")
	@ResponseBody
	public ResultMessage resetPwd(Long id, String pwd) {

		if (!isManager()) {
			return ResultUtil.result(ResultUtil.code_fail, "暂无权限");
		}
		Opter opter = opterService.findOpterById(id);

		if (opter == null) {
			return ResultUtil.result(ResultUtil.code_fail, "操作员信息不存在");
		}

		opter.setPwd(Md5Util.md5(pwd));
		opterService.update(opter);

		return ResultUtil.result(ResultUtil.code_success, "密码修改成功");
	}

	public boolean isManager() {
		Opter opter = null;
		String sessionId = getSessionId();
		SessionKey key = new WebSessionKey(sessionId, request, response);
		try {
			Session se = SecurityUtils.getSecurityManager().getSession(key);
			Object obj = se.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
			SimplePrincipalCollection coll = (SimplePrincipalCollection) obj;
			opter = (Opter) coll.getPrimaryPrincipal();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (opter != null && opter.getIsmanage() == 1) {
				return true;
			}
		}
		return false;
	}

}
